package com.aaa.service.impl;

import com.aaa.dao.UserinfoDao;
import com.aaa.entity.Userinfo;
import com.aaa.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AAATeacherChen
 * @since 2020-07-09
 */
@Service
//指定缓存的命名空间，相当于mybatis二级缓存中的命名空间
@CacheConfig(cacheNames = "chenjianCache")
public class UserinfoServiceImpl  implements UserinfoService {
    @Autowired
    private UserinfoDao userinfoDao;
    //此处的key是缓存的key
  //  @Cacheable(key = "'UserinfoServiceImpl_'+#userId",condition = "#userId%2==0")
    @Cacheable(key = "'UserinfoServiceImpl_'+#userId")
    @Override
    public Userinfo selectUserInfoById(Integer userId) {
        return userinfoDao.selectById(userId);
    }
    //缓存中有存在的key，cacheput会继续保存新的缓存，覆盖之前的缓存
    @CachePut(key = "'UserinfoServiceImpl_'+#userinfo.userid")
    @Override
    public Userinfo saveUserInfo(Userinfo userinfo) {
        Integer insert = userinfoDao.insert(userinfo);
        return userinfo;
    }
    @CachePut(key = "'UserinfoServiceImpl_'+#userinfo.userid")
    @Override
    public Userinfo updateUserInfo(Userinfo userinfo) {
        Integer insert = userinfoDao.updateById(userinfo);
        return userinfo;
    }
    //CacheEvict,根据key删除缓存
    @CacheEvict(key = "'UserinfoServiceImpl_'+#userId")
    @Override
    public Boolean deleteUserInfoById(Integer userId) {
        Integer insert = userinfoDao.deleteById(userId);
        if(insert>0){
            return true;
        }
        return false;
    }
}
